﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChessOnline
{
    class GamesManager
    {
        List<ChessOnlineService.GameInfo> GamesList = new List<ChessOnlineService.GameInfo>();
        string tempGameHash = "";
        ChessOnlineService.GameInfo currentGame = new ChessOnlineService.GameInfo();

        public GamesManager()
        {
            currentGame.Game_hash = "";
            currentGame.Creator_name = "";
            currentGame.Connected_name = "";
            currentGame.IsStarted = "false";
            currentGame.WinnerName = "";
            currentGame.ActivePlayerHash = "";
        }

        public delegate void EventHandler(object sender, object args);

        public event EventHandler ThrowGetGamesListCompleted = delegate { };

        public void GetGamesListAvailable()
        {
            ChessOnlineService.ChessOnlineServiceClient cl = new ChessOnlineService.ChessOnlineServiceClient();
            cl.GetGamesListAvailableCompleted += new EventHandler<ChessOnlineService.GetGamesListAvailableCompletedEventArgs>(GetGamesListAvailableCompleted);
            cl.GetGamesListAvailableAsync(Authorization.Instance.GetUserHash());
        }

        void GetGamesListAvailableCompleted(object sender, ChessOnlineService.GetGamesListAvailableCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                GamesList = e.Result;
                ThrowGetGamesListCompleted(sender, null);
            }
        }

        public List<ChessOnlineService.GameInfo> GetGamesList()
        {
            return GamesList;
        }

        public event EventHandler ThrowCreateJoinEnded = delegate { };

        public void CreateGame()
        {
            ChessOnlineService.ChessOnlineServiceClient cl = new ChessOnlineService.ChessOnlineServiceClient();
            cl.CreateNewGameCompleted += new EventHandler<ChessOnlineService.CreateNewGameCompletedEventArgs>(CreateNewGameCompleted);
            cl.CreateNewGameAsync(Authorization.Instance.GetUserHash());
        }

        void CreateNewGameCompleted(object sender, ChessOnlineService.CreateNewGameCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Получить информацию об игре
                tempGameHash = e.Result;
                ChessOnlineService.ChessOnlineServiceClient cl = new ChessOnlineService.ChessOnlineServiceClient();
                cl.UpdateGameInfoCompleted += new EventHandler<ChessOnlineService.UpdateGameInfoCompletedEventArgs>(UpdateGameInfoCompleted);
                cl.UpdateGameInfoAsync(Authorization.Instance.GetUserHash(), tempGameHash);
            }
        }

        void UpdateGameInfoCompleted(object sender, ChessOnlineService.UpdateGameInfoCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                currentGame = e.Result;
                ThrowCreateJoinEnded(sender, null);
            }
        }

        public string GetGameHash()
        {
            return currentGame.Game_hash;
        }

        public void JoinGame(string creator_name)
        {
            for (int i = 0; i < GamesList.Count; i += 1)
            {
                if (GamesList[i].Creator_name == creator_name)
                {
                    ChessOnlineService.ChessOnlineServiceClient cl = new ChessOnlineService.ChessOnlineServiceClient();
                    cl.JoinGameCompleted += new EventHandler<ChessOnlineService.JoinGameCompletedEventArgs>(JoinGameCompleted);
                    cl.JoinGameAsync(Authorization.Instance.GetUserHash(), GamesList[i]);
                    tempGameHash = GamesList[i].Game_hash;
                    i = GamesList.Count;
                }
            }
        }

        void JoinGameCompleted(object sender, ChessOnlineService.JoinGameCompletedEventArgs e)
        {
            if (e.Error == null && e.Result == "Player joined the game.")
            {
                // Получить информацию об игре
                ChessOnlineService.ChessOnlineServiceClient cl = new ChessOnlineService.ChessOnlineServiceClient();
                cl.UpdateGameInfoCompleted += new EventHandler<ChessOnlineService.UpdateGameInfoCompletedEventArgs>(UpdateGameInfoCompleted);
                cl.UpdateGameInfoAsync(Authorization.Instance.GetUserHash(), tempGameHash);
            }
        }

        public ChessOnlineService.GameInfo GetGameInfo()
        {
            return currentGame;
        }

    }
}
